package com.vite.web.controller.system;

import com.vite.common.annotation.Log;
import com.vite.common.annotation.RepeatSubmit;
import com.vite.common.constant.CommonConstants;
import com.vite.common.core.controller.BaseController;
import com.vite.common.core.domain.AjaxResult;
import com.vite.common.core.domain.entity.SysNotice;
import com.vite.common.core.page.TableDataInfo;
import com.vite.common.enums.BusinessType;
import com.vite.sse.service.SseService;
import com.vite.system.service.ISysNoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    private final ISysNoticeService noticeService;

    private final SseService sseService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermission('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId,getUserId());
        //查看单发socket消息
        if(CommonConstants.NOTICE_READ_STATUS.READ_UNDO.equals(sysNotice.getReadStatus())){
            sseService.redoNotice(sysNotice.getNoticeType(),getUsername());
        }
        return success(sysNotice);
    }

    /**
     * 获取未读公告数量
     */
    @GetMapping(value = "/getUnReadNumb")
    public AjaxResult getUnReadNumb()
    {
        return success(noticeService.getUnReadNumb());
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        int num = noticeService.insertNotice(notice);
        //新增公告群发socket消息
        sseService.sendNewNotice(notice.getNoticeType(),getUsername());
        return toAjax(num);
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    /**
     * 重新推送公告
     */
    @PreAuthorize("@ss.hasPermission('system:notice:rePush')")
    @Log(title = "公告已读记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/rePushNotice/{noticeId}/{noticeType}")
    public AjaxResult rePushNotice(@PathVariable Long noticeId,@PathVariable String noticeType)
    {
        //重新推送群发socket消息
        sseService.sendRePush(noticeId,noticeType,getUsername());
        int row = noticeService.rePushNotice(noticeId);
        return toAjax(row);
    }

}
